package com.junzhiit.nengji.shop.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.TransactionDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.Transaction;
import com.junzhiit.nengji.model.User;
import com.junzhiit.nengji.shop.service.TransactionService;
import com.junzhiit.nengji.shop.web.exception.OrdersNotFoundException;
import com.junzhiit.nengji.shop.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.shop.web.exception.UserNotFoundException;
import com.junzhiit.nengji.shop.web.vo.ResultVO;
import com.junzhiit.nengji.shop.web.vo.TransactionAddVO;

@Service
public class TransactionServiceImpl extends BaseServiceImpl implements TransactionService {

	@Autowired
	private TransactionDAO transactionDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;
	
	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private UserDAO userDAO;

	@Override
	public ResultVO add(TransactionAddVO transactionAddVO) {
		ResultVO result = new ResultVO();
		
		Orders orders= ordersDAO.query(transactionAddVO.getOrderId());
		if(orders==null){
			throw new OrdersNotFoundException("订单没找到");
		}
		
		Shop shop= shopDAO.query(transactionAddVO.getShopId());
		if(shop==null){
			throw new ShopNotFoundException("点铺没找到");
		}
		
		User user=userDAO.query(transactionAddVO.getUserId());
		if(user==null){
			throw new UserNotFoundException("用户没找到");
		}
		
		Transaction transaction = new Transaction();
		BeanUtils.copyProperties(transactionAddVO, transaction);
		transactionDAO.insert(transaction);
		return result;
	}

}
